package cn.iocoder.yudao.module.shop.controller.admin.statistics;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.admin.wallet.vo.recharge.PayWalletRechargePageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.opthistory.vo.ShopOptHistoryPageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.order.vo.ShopOrderPageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.statistics.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.statistics.ShopUserStatisticsDO;
import cn.iocoder.yudao.module.shop.service.statistics.ShopStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 店铺统计")
@RestController
@RequestMapping("/shop/statistics")
@Validated
public class ShopStatisticsController {

    @Resource
    private ShopStatisticsService shopStatisticsService;


    @GetMapping("/user")
    @Operation(summary = "获得灸师统计数据")
    @Parameter(name = "userId", description = "编号", required = true, example = "1024")
    public CommonResult<ShopUserStatisticsDO> getUser(@RequestParam("userId") Long userId) {
        ShopUserStatisticsDO userStatisticsDO = shopStatisticsService.getUserStatistics(userId);
        return success(userStatisticsDO);
    }

    @PostMapping("/users")
    @Operation(summary = "获得灸师统计数据")
    public CommonResult<List<ShopUserStatisticsDO>> getUsers(@RequestBody ShopUserStatisticsReqVO reqVO) {
        List<ShopUserStatisticsDO> list = new ArrayList<>();
        for(int i = 0; i<reqVO.getUserIds().size(); i++){
            list.add(shopStatisticsService.getUserStatistics(reqVO.getUserIds().get(i)));
        }
        return success(list);
    }

    @PostMapping("/jiuliang")
    @Operation(summary = "获得灸量统计数据")
    public CommonResult<ShopJiuliangStatisticsRespVO> getJiuliang(@Valid @RequestBody ShopOrderPageReqVO reqVO) {
        ShopJiuliangStatisticsRespVO respVO = shopStatisticsService.getJiuliangStatistics(reqVO.getTenantId(), reqVO.getStartTime()[0], reqVO.getStartTime()[1]);
        return success(respVO);
    }

    @PostMapping("/recharge")
    @Operation(summary = "充值记录统计数据")
    public CommonResult<ShopRechargeStatisticsRespVO> getRecharge(@Valid @RequestBody PayWalletRechargePageReqVO reqVO){
        ShopRechargeStatisticsRespVO respVO = shopStatisticsService.getRechargeStatistics(reqVO.getTenantId(), reqVO.getPayTime()[0], reqVO.getPayTime()[1]);
        return success(respVO);
    }

    @PostMapping("/jiucase")
    @Operation(summary = "案例记录统计数据")
    public CommonResult<ShopJiucaseStatisticsRespVO> getJiucase(@Valid @RequestBody ShopOptHistoryPageReqVO reqVO){
        ShopJiucaseStatisticsRespVO respVO = shopStatisticsService.getJiucaseStatistics(reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        return success(respVO);
    }

    @PostMapping("/salary")
    @Operation(summary = "工资统计")
    public CommonResult<ShopSalaryStatisticsRespVO> getSalary(@Valid @RequestBody ShopSalaryStatisticsReqVO reqVO){
        DateTime time = new DateTime(reqVO.getDateTime());
        DateTime startTime = DateUtil.beginOfMonth(time);
        DateTime endTime = DateUtil.endOfMonth(time);
        return success(shopStatisticsService.getSalaryDetail(startTime.toLocalDateTime(), endTime.toLocalDateTime(), reqVO.getTenantId()));
    }

    @PostMapping("/detail")
    @Operation(summary = "店铺整体统计")
    public CommonResult<ShopStatisticsRespVO> getShopStatistics(@Valid @RequestBody ShopSalaryStatisticsReqVO reqVO){
        DateTime time = new DateTime(reqVO.getDateTime());
        DateTime startTime = DateUtil.beginOfMonth(time);
        DateTime endTime = DateUtil.endOfMonth(time);

        return success(shopStatisticsService.getShopDetail(startTime.toLocalDateTime(), endTime.toLocalDateTime(), reqVO.getTenantId()));
    }

    @PostMapping("/day")
    @Operation(summary = "店铺某一天统计")
    public CommonResult<ShopDayStatisticsRespVO> getDayStatistics(@Valid @RequestBody ShopDayStatisticsReqVO reqVO){
        return success(shopStatisticsService.getShopDayDetail(reqVO.getDateTime(), reqVO.getTenantId()));
    }
}
